package BasicEngine
{
	import DrawEngine.DrawInfo;
	import DrawEngine.DrawManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class LoaderManager
	{
		public static var LoadTaskList:Vector.<LoadUnit>;
		public static var Runing:Boolean;
		public static var imgLoader:Loader;		
		public static var xmlLoader:URLLoader;
			
		public function LoaderManager()
		{
		}
		
		public static function Init():void
		{
			LoadTaskList = new Vector.<LoadUnit>();
			Runing = false;
			imgLoader = new Loader();
			xmlLoader = new URLLoader();
		}			
		
		//开始下载，如果已经有下载的任务队列在执行，则取消
		public static function StartLoadUnit():Boolean
		{
			if (!Runing)
			{
			     Runing = true;
			     imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,OnLoadImg);
			     xmlLoader.addEventListener(Event.COMPLETE,OnLoadXml);	
			     imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,OnLoading);
			     LoadStep();
				 return true;
			}
			else
			{
				return false;
			}
		}
		
		public static function AddTask(_task:LoadUnit):void
		{
			LoadTaskList.push(_task);			
		}
		
		public static function LoadStep():void
		{
			if(LoadTaskList.length == 0)
			{
				Runing = false;
				imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,OnLoadImg);
				imgLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,OnLoading);
				xmlLoader.removeEventListener(Event.COMPLETE,OnLoadXml);
				return;
			}
            var task:LoadUnit = LoadTaskList.shift();
			imgLoader.name = task.NameKey;
			trace(task.ImgUrl);
			imgLoader.load(new URLRequest(task.ImgUrl));
			trace(task.XmlUrl);
			xmlLoader.load(new URLRequest(task.XmlUrl));
		}
		
		private static function OnLoadImg(e:Event):void
		{			
			var loader2:Loader = (e.target.loader) as Loader;			
			var bmpData:BitmapData = (loader2.content as Bitmap).bitmapData;
			DrawManager.SourcePool[loader2.name] = bmpData;
			trace(loader2.name, ":", bmpData.width,",", bmpData.height);
			LoadStep();
		}
		
		private static function OnLoadXml(e:Event):void
		{
			trace(imgLoader.name,":xml load completed");
			var loader:URLLoader = e.target as URLLoader;
			
			var xml:XML = new XML(loader.data);
			
			var key:String = imgLoader.name;
			var i:int = 0;
			
			for each(var item:XML in xml.Frame)
			{
				var el:XML = xml.Frame[i];
				
				var id:int = parseInt(el.@Index[0]);
				var x:int = parseInt(el.@X[0]);
				var y:int = parseInt(el.@Y[0]);
				var w:int = parseInt(el.@Width[0]);
				var h:int = parseInt(el.@Height[0]);
				var ox:int = parseInt(el.@OffsetX[0]);
				var oy:int = parseInt(el.@OffsetY[0]);
				
				var info:DrawInfo = new DrawInfo(key,id,x,y,w,h,ox,oy);
				DrawManager.SinglePool[id] = info;
				i++;
			}
		}
		
		private static function OnLoading(e:ProgressEvent):void
		{
			trace(e.bytesTotal,"--",e.bytesLoaded);
		}
	}
}

